<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Product;
use App\Services\ZeroJoinCheckService;
use Illuminate\Http\Request;

class ZeroProductController extends BaseController
{
    public $model = Product::class;

    public function index(Request $request) {
        $search = ['on_status' => 1];
        $input = $request->input();
        $order_by_key = $request->input('order_by_key', 'id');
        $order_by_value = $request->input('order_by_value', 'desc');

        $list = Product::apply($search, $input)
            ->select(['id', 'title', 'video_url', 'image_url', 'producer_id', 'is_full_reduction'])
            ->where('is_zero', 1)
            ->orderBy($order_by_key, $order_by_value)
            ->paginate(per_page());

        $extra['satisfy'] = ZeroJoinCheckService::handle($this->getUser());
        return $this->paginate($list, $extra);
    }

    public function info(Request $request)
    {
        $id = \request("id");
        if (!$id) {
            return $this->failed("参数ID不存在");
        }
        $info = Product::select(['id', 'title', 'video_url', 'image_url', 'producer_id', 'is_zero', 'on_status'])->find($id);
        if (!$info || $info->on_status != 1)
            return $this->failed('商品不存在');

        return $this->success($info);
    }
}

